<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactMessageBatchSend\Action\Workbench\Traits;

use Hyperf\Di\Annotation\Inject;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkContact\Contract\WorkContactTagPivotContract;

trait ContactTrait
{
    /**
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeService;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactService;

    /**
     * @Inject
     * @var WorkContactTagPivotContract
     */
    protected $workContactTagPivotService;

    private function filterContact(array $params): array
    {
        // 全部客户-员工、添加时间筛选
        $contacts = $this->workContactEmployeeService->getWorkContactEmployeesByEmployeeIdsCreateTime($params['employeeIds'], $params['addTimeStart'], $params['addTimeEnd'], ['contact_id']);
        if (empty($contacts)) {
            return [];
        }
        $contactIds = array_unique(array_column($contacts, 'contactId'));
        // 跟进状态筛选
        if (! empty($params['followUpStatusIds'])) {
            $contacts = $this->workContactService->getWorkContactsByIdsFollowUpStatus($contactIds, $params['followUpStatusIds'], ['id']);
            if (empty($contacts)) {
                return [];
            }
            $contactIds = array_unique(array_column($contacts, 'id'));
        }
        // 标签筛选
        if (! empty($params['tags'])) {
            // 标签满足其一
            if ($params['tagsType'] === 1) {
                $contacts = $this->workContactTagPivotService->getWorkContactTagPivotsByContactIdsEmployeeIdsTagIds($contactIds, $params['employeeIds'], $params['tags'], 1, ['contact_id']);
                if (empty($contacts)) {
                    return [];
                }
                $contactIds = array_unique(array_column($contacts, 'contactId'));
            }
            // 标签同时满足
            if ($params['tagsType'] === 2) {
                $contacts = $this->workContactTagPivotService->getWorkContactTagPivotsByContactIdsEmployeeIdsTagIds($contactIds, $params['employeeIds'], $params['tags'], 2, ['contact_id']);
                if (empty($contacts)) {
                    return [];
                }
                $contactIds = array_unique(array_column($contacts, 'contactId'));
            }
            // 无标签
            if ($params['tagsType'] === 3) {
                $contacts = $this->workContactTagPivotService->getWorkContactTagPivotsByContactIdsEmployeeIdsTagIds($contactIds, $params['employeeIds'], $params['tags'], 3, ['contact_id']);
                if (empty($contacts)) {
                    return [];
                }
                $invalidContactIds = array_unique(array_column($contacts, 'contactId'));
                $contactIds = array_diff($contactIds, $invalidContactIds);
            }
        }

        if (! empty($params['removeTags'])) {
            $contacts = $this->workContactTagPivotService->getWorkContactTagPivotsByContactIdsEmployeeIdsTagIds($contactIds, $params['employeeIds'], $params['removeTags'], 4, ['contact_id']);
            if (empty($contacts)) {
                return [];
            }
            $contactIds = array_unique(array_column($contacts, 'contactId'));
        }
        return array_merge($contactIds);
    }
}
